#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>



#include "lvgl/lvgl.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include "lv_lib_png/lv_png.h"
#include "lv_menu/boot_animation/boot_animation.h"
#include "lv_menu/init_icon/init_icon.h"

#define DISP_BUF_SIZE (480 * 854)


/*
终端执行 cat /sys/class/net/wlan0/operstate
如果wifi连接成功，会输出:up
*/
void waitWifiConnectSuccess(void){

    char path[100];
    char *line = NULL;
    int len = 0;
    int read = 0;
	int count = 0;
	int wifiConnectSuccess = -1;

	LV_LOG_USER("enter");


	while(1){
		sleep(1); // 休眠1秒
		// 使用popen执行命令
		FILE *fp = popen("cat /sys/class/net/wlan0/operstate", "r");
		if (fp == NULL) {
			// 关闭命令执行流
    		pclose(fp);
			printf("Failed to run command\n");
			break;
		}
		char buffer[1024] = {0, };
		if(fgets(buffer, sizeof(buffer), fp) == NULL){
			// 关闭命令执行流
    		pclose(fp);
			printf("Failed to read\n");
			break;
		}
		// 关闭命令执行流
		pclose(fp);

		//LV_LOG_USER("read=%d, len=%d", read, len);
	    //buffer[len] = '\0'; // 确保字符串以null结束
	    LV_LOG_USER("buffer=%s", buffer);
	    
	    // 检查operstate是否为up，表示已连接
	    if (strstr(buffer, "up") != NULL) {
			wifiConnectSuccess = 0;
	        break; // WiFi已连接
	    }


		//2025.3.31 实测要26秒才能检测到 up
		count++;
		if(count >= 40){
			LV_LOG_USER("wait over time, fail");
			break;
		}
		
	}


	if(wifiConnectSuccess == 0){
		LV_LOG_USER("wifi connect success");
		system("udhcpc -i wlan0");//自动获取ip
	}else{
		LV_LOG_USER("wifi connect fail");
	}
	LV_LOG_USER("exit");

}

int main(void)
{
    /*LittlevGL init*/
    lv_init();

    /*Linux frame buffer device init*/
    fbdev_init();

    /*A small buffer for LittlevGL to draw the screen's content*/
    static lv_color_t buf[DISP_BUF_SIZE];

    /*Initialize a descriptor for the buffer*/
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);

    /*Initialize and register a display driver*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf   = &disp_buf;
    disp_drv.flush_cb   = fbdev_flush;
    disp_drv.hor_res    = 480;
    disp_drv.ver_res    = 854;
    lv_disp_drv_register(&disp_drv);

	/* Linux input device init */
    evdev_init();
	
    /* Initialize and register a display input driver */
    lv_indev_drv_t indev_drv;
    lv_indev_drv_init(&indev_drv);      /*Basic initialization*/

    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = evdev_read;   //lv_gesture_dir_t lv_indev_get_gesture_dir(const lv_indev_t * indev)
    lv_indev_t * my_indev = lv_indev_drv_register(&indev_drv); 

    // 支持png
    lv_png_init();

    // Set Image Cache size
    lv_img_cache_set_size(32);


	//waitWifiConnectSuccess();

    /* 初始化桌面环境 */
    lv_100ask_boot_animation(lv_100ask_demo_init_icon, 3000); //开机动画


    while(1) {
        lv_task_handler();
        usleep(5000);
    }
    return 0;
}


/*Set in lv_conf.h as `LV_TICK_CUSTOM_SYS_TIME_EXPR`*/
uint32_t custom_tick_get(void)
{
    static uint64_t start_ms = 0;
    if(start_ms == 0) {
        struct timeval tv_start;
        gettimeofday(&tv_start, NULL);
        start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
    }

    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);
    uint64_t now_ms;
    now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

    uint32_t time_ms = now_ms - start_ms;
    return time_ms;
}
